package com.cwises.service.system.user;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cwises.dao.system.user.UserDao;
import com.cwises.entity.system.user.MsgCode;
import com.cwises.entity.system.user.UserInfoManager;
import com.orm.AbstractBaseEntityOperation;
import com.orm.IBaseEntityOperation;
import com.util.GetConfig;
import com.util.Util;
import com.util.sendmsg.SendMsg;

@Service("userService")
@Transactional
public class UserServiceImp extends AbstractBaseEntityOperation<UserInfoManager> implements UserService {
	@Resource(name = "userDao")
	private UserDao dao;
	
	@Override
	public IBaseEntityOperation<UserInfoManager> getDao() {
		return  dao;
	}
	
	@Override
	public int getUserInfoForUpdatePwd(UserInfoManager userInfo) {
		if(!"".equals(userInfo.getUserId())&&userInfo.getUserId()!=null){
			return dao.findObjectByCondition("getUserInfoForUpdatePwd",userInfo);
		}else{
			return dao.findObjectByCondition("getUserInfoForValidatePwd",userInfo);
		}
	}
	@Override
	public boolean delete(String id) {
		dao.deleteByConditions("deleteUserMage",id);
		dao.deleteByConditions("deleteUser",id);
		return true;
	}
	@Override
	public void deleteBatch(String[] ids) {
		Map<String,Object> map= new HashMap<String,Object>();
		map.put("ids", ids);
		dao.deleteByConditions("deleteBatchMage",map);
		dao.deleteByConditions("deleteBatch",map);
	}
	
	@Override
	public void addUserInfoAndData(UserInfoManager userInfoManager) {
		dao.updateByCondition("insertUserInfo",userInfoManager);
	}

	@Override
	public void updateUser(UserInfoManager userInfo) {
		dao.updateByCondition("updateUserInfo",userInfo);
	}
	
	@Override
    public String validateCode(HttpSession session,String type,String telephone,String code) {
		if(session==null||StringUtils.isEmpty(type)||StringUtils.isEmpty(telephone)||StringUtils.isEmpty(code)){
			return "nullInfo";
		}else{
			MsgCode msgCode=(MsgCode)session.getAttribute(type+telephone);
			if(msgCode==null){
				return "nullCode";
			}
			long time=msgCode.getTime();
			int overTimeIng=Integer.parseInt(GetConfig.getString("overTime"));
			if(time<(System.currentTimeMillis()-overTimeIng*60*1000l)){
				return "timeOut";
			}
			String vCode=msgCode.getCode();
			if(code.equals(vCode)){
				return "success";
			}else{
				return "codeError";
			}
		}
    }

	 /**
     * @Description: 生成验证码并发送短信。
     * 
     */
	@Override
	public String createIdentifyingCode(HttpSession httpSession,String type,String telephone) {
		String result="success";
		MsgCode msgCode=(MsgCode)httpSession.getAttribute(type+telephone);
		//获得配置文件中的超时时间参数
		if(msgCode!=null){
			long time=msgCode.getTime();
			//60秒不能重复发送
			if(time>(System.currentTimeMillis()-60*1000l)){
				result="haveSend";
			}
		}
		String overTime = GetConfig.getString("overTime");
		String code=Util.getCode();
		//发送短信
		result=SendMsg.transmitSMS(telephone,code,overTime);
		//发送成功记录session
		if("success".equals(result)){
			msgCode=new MsgCode(code,System.currentTimeMillis());
			httpSession.setAttribute(type+telephone, msgCode);
		}
		return result;
	}

	@Override
	public void updatePwd(String id, String newPass) {
		UserInfoManager userInfo=new UserInfoManager();
		userInfo.setUserId(id);
		userInfo.setUserPwd(Util.MD5(newPass));
		userInfo.setUpdateTime(new Date());
		userInfo.setUpdatePerson(id);
		userInfo.setIsReserved("1");
		dao.updateByCondition("updateUserPwd",userInfo);
	}
}
